package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.SPUAddNewParam;
import cn.tedu.csmall.product.pojo.vo.SPUStandardVO;
import cn.tedu.csmall.product.service.SPUService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/spu/")
@Validated
@Api(tags = "08. SPU管理模块")
public class SPUController {

    @Autowired
    private SPUService spuService;

    public SPUController() {
        log.info("创建控制器对象：SPUController");
    }

    // http://localhost:8080/spu/add-new
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    @ApiOperation("添加SPU")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid SPUAddNewParam spuAddNewParam) {
        log.debug("开始处理【添加SPU】的请求，参数：{}", spuAddNewParam);
        spuService.addNew(spuAddNewParam);
        return JsonResult.success();
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @ApiOperation("查询SPU列表")
    @ApiOperationSupport(order = 400)
    public JsonResult list() {
        log.debug("开始处理【查询SPU列表】的请求");
        return JsonResult.success(spuService.list());
    }

    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/pms/product/read')")
    @ApiOperation("根据ID查询SPU信息")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "SPU ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的SPU ID值！") Long id) {
        log.debug("开始处理【根据ID查询SPU信息】的请求，参数：{}", id);
        SPUStandardVO spu = spuService.getStandardById(id);
        return JsonResult.success(spu);
    }

    @GetMapping("/{id:[0-9]+}/full-info")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @ApiOperation("根据ID查询一条SPU的全部信息")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long")
    public JsonResult getFullInfoById(@PathVariable Long id) {
        log.debug("开始处理【根据ID查询一条SPU的全部信息】的请求，参数{}", id);
        return JsonResult.success(spuService.getFullInfoById(id));
    }

}
